import axios from "axios";
import { ElMessage } from "element-plus";
import { localCache } from "./cache";
import { TOKEN } from "../global/constans"

const intance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
});

intance.interceptors.request.use(
  (config) => {
    // 添加token
    // const token = localCache.getCache(TOKEN);
    // if (token) {
    //   config.headers.Authorization = `Bearer ${token}`;
    // }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

intance.interceptors.response.use(
  (response) => {
    const { code, data } = response.data;
    if (code !== 0) {
      ElMessage.error(data.message);
      return Promise.reject(data.message);
    }
    return data;
  },
  (error) => {
    const { message } = error;
    if (message === "Network Error") {
      ElMessage.error("后端接口连接异常");
    }
  }
);
export default intance;
